﻿using System;
using System.Linq.Expressions;
using System.Reflection;

namespace WeiboPlus.Models
{
    public static class PropertySupport
    {
        public static string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
        {
            if (propertyExpression == null)
            {
                throw new ArgumentNullException("propertyExpression");
            }

            MemberExpression expression = propertyExpression.Body as MemberExpression;

            if (expression == null)
            {
                throw new ArgumentException(R.PropertySupport_NotMemberAccessExpression_Exception, "propertyExpression");
            }

            PropertyInfo info = expression.Member as PropertyInfo;

            if (info == null)
            {
                throw new ArgumentException(R.PropertySupport_ExpressionNotProperty_Exception, "propertyExpression");
            }

            if (info.GetGetMethod(true).IsStatic)
            {
                throw new ArgumentException(R.PropertySupport_StaticExpression_Exception, "propertyExpression");
            }

            return expression.Member.Name;
        }
    }
}
